import Excel from 'exceljs/dist/es5/exceljs.browser'
import fileDownload from 'js-file-download'
import $ from 'jquery'

export class TrueExcel {
  constructor() {
    this.workbook = new Excel.Workbook()
    this.workbook.created = new Date()
    this.sheet = this.workbook.addWorksheet('My Sheet')
    this.head = null
    this.imageCol = 0
  }
  addElTable(table) {
    this.workbook = new Excel.Workbook()
    this.workbook.created = new Date()
    this.sheet = this.workbook.addWorksheet('My Sheet')
    this.head = null
    this.addTableHeader(table)
    this.addTableBody(table)
  }
  addTableHeader(table) {
    const headerArray = []
    for (const item of $(table.$el).find('.el-table__header th')) {
      const head = {}
      head.header = item.innerText
      head.key = item.innerText
      head.width = 30
      headerArray.push(head)
    }
    if (headerArray.length > 0) {
      this.sheet.columns = headerArray
      this.sheet.getRow(1).eachCell((cell, cellNum) => {
        if (!cell) console.log(cellNum)
        cell.fill = {
          type: 'pattern',
          pattern: 'solid',
          fgColor: { argb: 'eeeeee' }
        }
      })
    }
  }
  addTableBody(table) {
    for (const row of $(table.$el).find('.el-table__body tr')) {
      const rowArray = []
      for (const item of $(row).find('td')) {
        if (item.innerText === '') {
          rowArray.push(item.innerText)
        } else {
          rowArray.push(
            Number(item.innerText) || Number(item.innerText) === 0
              ? Number(item.innerText)
              : item.innerText
          )
        }
      }
      this.sheet.addRow(rowArray)
    }
  }
  removeOperateRow() {
    const operateRow = this.sheet.getColumn('操作')
    this.sheet.spliceColumns(operateRow.number, 1)
  }
  removeSelectRow() {
    // const operateRow = this.sheet.getColumn('操作')
    this.sheet.spliceColumns(1, 1)
  }
  addImage(img) {
    const lastCol = this.sheet.columns.length
    const imgId = this.workbook.addImage({
      base64: img.src,
      extension: 'png'
    })
    this.sheet.addImage(imgId, {
      tl: {
        col: lastCol,
        row: this.imageCol
      },
      br: {
        col: img.width / 5 / 19.2 + lastCol,
        row: img.height / 5 / 5 + this.imageCol
      },
      editAs: 'oneCell'
    })
    this.imageCol += img.height / 5 / 5 + 5
  }
  export() {
    this.workbook.xlsx
      .writeBuffer({
        base64: true
      })
      .then(function (xls64) {
        fileDownload(xls64, 'export.xlsx')
      })
      .catch(err => {
        console.log(err)
      })
  }
}

// export class DownloadExcel {
//   trueExcel() {
//     this.workbook = new Excel.Workbook()
//     this.sheet = this.workbook.addWorksheet('Sheet one')

//     this.workbook.creator = '能源管理系统'
//     this.workbook.created = new Date()
//     this.workbook.modified = new Date()
//   }
//   tabelExportToExcel(data) {
//     this.sheet.properties.defaultRowHeight = 25
//     this.sheet.columns = []
//     const col = Object.keys(data[0])

//     if (data[0].label) {
//       col.forEach(v => {
//         const columnsInfo = {}
//         data.forEach(v => {
//           columnsInfo.header = v.label
//         })
//         columnsInfo.key = v
//         columnsInfo.width = 20
//         this.sheet.columns.push(columnsInfo)
//       })
//     } else {
//       col.forEach(v => {
//         const columnsInfo = {}
//         columnsInfo.header = v
//         columnsInfo.key = v
//         columnsInfo.width = 20
//         this.sheet.columns.push(columnsInfo)
//       })
//     }

//     data.forEach(item => {
//       this.sheet.addRow(item)

//     })
//   }
//   imageExportToExcel() {}

//   downloadExcel() {

//     this.workbook.xlsx.writeBuffer({
//       base64: true
//     }).then((xls64) => {
//       // 构建锚标记和附加文件（在chrome中工作）
//       var a = document.createElement('a')
//       var data = new Blob([xls64], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })

//       var url = URL.createObjectURL(data)
//       a.href = url
//       a.download = Date.now() + 'export.xlsx'
//       document.body.appendChild(a)
//       a.click()
//       setTimeout(() => {
//         document.body.removeChild(a)
//         window.URL.revokeObjectURL(url)
//       }, 0)
//     }).catch((error) => {

//     })
//   }
// }

// // -----------------------------------------------------------------------
// export class Download {
//   tabelExportToExcel(data) {
//     const workbook = new Excel.Workbook()
//     workbook.creator = '能源管理系统'
//     workbook.created = new Date()
//     workbook.modified = new Date()

//     const sheet = workbook.addWorksheet('My Sheet')
//     sheet.properties.defaultRowHeight = 25

//     sheet.columns = [
//       { header: '时间', key: 'date', width: 20 },
//       { header: '姓名', key: 'name', width: 10 },
//       { header: '地址', key: 'address', width: 40 }
//     ]
//     data.forEach(item => {
//       sheet.addRow(item)

//     })

//     workbook.xlsx.writeBuffer({
//       base64: true
//     }).then(xls64 => {
//       // build anchor tag and attach file (works in chrome)
//       // 构建锚标记和附加文件（在chrome中工作）
//       var a = document.createElement('a')
//       var data = new Blob([xls64], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })

//       var url = URL.createObjectURL(data)
//       a.href = url
//       a.download = 'export.xlsx'
//       document.body.appendChild(a)
//       a.click()
//       setTimeout(() => {
//         document.body.removeChild(a)
//         window.URL.revokeObjectURL(url)
//       }, 0)
//     }).catch(error => {

//     })
//   }
// }
